<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/6/26
 * Time: 20:25
 */
namespace app\api\controller;
use app\models\Models;

class Business extends Base {
    /**
     * 现场
     * @return \think\response\Json
     */
    public function live(){
        if(request()->isPost()){
            $post = input('param.');
            $admin = new Models('admin');
            try{
                $params = self::paramsSign(['equipment','encrypt','lng','lat','location']);
                // 登录状态下
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                    // 查询用户关注商家的id
                    $follow = new Models('follow');
                    $list = $follow->_select(['fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_BUSINESS,'status'=>Models::FOLLOW_YES],'id');
                    $ids  = array_column($list,'id');
                    // 更新用户位置信息
                    self::updateLocation(['lng'=>$params['lng'],'lat'=>$params['lat'],'location'=>$params['location']],['uid'=>$params['uid']]);
                }
                $join  = [['business b','a.id = b.id','left']];
                $field = 'b.id,b.name,b.background,b.fans_num,b.people_num,b.lng,b.lat';
                $list = $admin->joinMany($join,['a.status'=>Models::STATUS_ON,'b.id'=>['<>',Models::OFFICIAL]],$field);
                foreach($list as $k=>$v){
                    $list[$k]['background'] = empty($v['background']) ? '' : self::arrayMapBackground($v['background']);
                    $list[$k]['distance']   = getDistance($v['lng'],$v['lat'],$post['lng'],$post['lat']);
                    $list[$k]['is_follow']  = empty($ids) ? Models::FOLLOW_NOT : (in_array($v['id'],$ids) ? Models::FOLLOW_YES : Models::FOLLOW_NOT);
                    unset($list[$k]['lng'],$list[$k]['lat']);
                }
                $distance = array_column($list,'distance');   // 获取列数据
                $fans_num = array_column($list,'fans_num');   // 获取列数据
                array_multisort($distance,SORT_ASC,$fans_num,SORT_DESC,$list);    // 多维数组的排序
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 现场详情
     * @return \think\response\Json
     */
    public function liveShow(){
        if(request()->isPost()){
            $params = input('param.');
            try{
                // 验证签名
                self::sign($params);
                $business = new Models('business');
                $info = $business->_find(['id'=>$params['business_id']],'id,video,image,descrip,location');
                $info['video'] = empty($info['video']) ? '' : self::arrayMapVideo($info['video']);
                $info['image'] = empty($info['image']) ? '' : array_map([$this,'arrayMapLive'],json_decode($info['image'],true));
                // 显示最近一场活动
                $activity = new Models('activity');
                $list = $activity->_select(['bid'=>$params['business_id'],'status'=>['<>',Models::STATUS_OFF]],'id,name,image,join_num,join_min,begin_time,status','id desc',1);
                $info['activity'] = current($list);
                $info['activity']['image']   = empty($info['activity']['image']) ? '' : array_map([$this,'arrayMapActivity'],json_decode($info['activity']['image'],true))[0];
                $info['activity']['address'] = $info['location'];
                unset($info['location']);
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($info));
        }
    }
}